home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue79 / misc / tougher / reply.cpp.txt < prev    next >
Encoding:
Text File  |  2002-08-14  |  3.1 KB  |  152 lines

  1. //
  2. //
  3. // Copyright 2002 Rob Tougher <robt@robtougher.com>
  4. //
  5. // This file is part of xmlrpc.
  6. //
  7. // xmlrpc is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation; either version 2 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // xmlrpc is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with xmlrpc; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20. //
  21. //
  22.  
  23.  
  24. // Implementation of the reply class
  25.  
  26.  
  27. #include "reply.hpp"
  28. #include "xmlrpc_exceptions.hpp"
  29.  
  30. namespace xmlrpc
  31. {
  32.   reply::reply ( const std::string value ) : m_value ( value ) {}
  33.  
  34.   reply::~reply() {}
  35.  
  36.   //
  37.   // <reply>
  38.   //   <return_value></return_value>
  39.   //   <errors>
  40.   //      <error></error>
  41.   //   </errors>
  42.   // </reply>
  43.   //
  44.   std::string reply::get_xml() const
  45.   {
  46.  
  47.     xml::node root ( "reply" );
  48.     xml::node * return_value = root.add_child ( "return_value", m_value );
  49.     xml::node * errors = root.add_child ( "errors" );
  50.  
  51.     for ( std::vector<std::string>::const_iterator it = m_errors.begin();
  52.       it != m_errors.end();
  53.       it++ )
  54.       {
  55.     xml::node * error = errors->add_child ( "error", *it );
  56.       }
  57.  
  58.     return root.get_xml();
  59.   }
  60.  
  61.  
  62.  
  63.   void reply::load_xml ( const std::string s )
  64.   {
  65.  
  66.     m_errors.clear();
  67.     m_value = "";
  68.  
  69.     xml::node n;
  70.  
  71.     try
  72.       {
  73.     n.load_xml ( s );
  74.       }
  75.     catch ( xml::parse_exception& e )
  76.       {
  77.     throw reply_exception ( "Could not parse the reply." );
  78.       }
  79.  
  80.     if ( n.get_child_count() > 0 )
  81.       {
  82.     xml::node * return_value = n.get_child ( 0 );
  83.  
  84.     if ( return_value &&
  85.          return_value->get_name().compare ( "return_value" ) == 0 )
  86.       {
  87.         xml::node * xmlroot = return_value->get_child ( 0 );
  88.  
  89.         if( xmlroot )
  90.           {
  91.         m_value = xmlroot->get_xml();
  92.           }
  93.         else
  94.           {
  95.         m_value = return_value->get_text();
  96.           }
  97.       }
  98.     else
  99.       {
  100.         throw reply_exception
  101.           ( "<return_value> is not the first child node in the reply." );
  102.  
  103.       }
  104.  
  105.     xml::node * errors = n.get_child ( 1 );
  106.  
  107.     if ( errors )
  108.       {
  109.         int error_count = errors->get_child_count();
  110.  
  111.         for ( int error_index = 0; error_index < error_count; error_index++ )
  112.           {
  113.         xml::node * error = errors->get_child ( error_index );
  114.  
  115.         if ( error && error->get_name().compare ( "error" ) == 0 )
  116.           {
  117.             xml::node * xmlroot = error->get_child ( 0 );
  118.  
  119.             if ( xmlroot )
  120.               {
  121.             m_errors.push_back ( error->get_xml() );
  122.               }
  123.             else
  124.               {
  125.             m_errors.push_back ( error->get_text() );
  126.               }
  127.           }
  128.           }
  129.       }
  130.     else
  131.       {
  132.         throw reply_exception
  133.           ( "Reply does not contain an errors collection." );
  134.       }
  135.       }
  136.   }
  137.  
  138.  
  139.  
  140.   std::string reply::get_error ( const int index ) const
  141.   {
  142.     if ( m_errors.size() > index )
  143.       {
  144.     return * ( m_errors.begin() + index );
  145.       }
  146.     else
  147.       {
  148.     return std::string ( "" );
  149.       }
  150.   }
  151. };
  152.